package com.example.myweba;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.myweba.common.WriteHandlingWebResourceRequest;
import com.example.myweba.common.WriteHandlingWebViewClient;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    WebView webView;
    SwipeRefreshLayout swipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new StatusBarFull().setStatusBarFullTransparent(getWindow());
        setContentView(R.layout.activity_main);
        //获得控件
        webView = (WebView) findViewById(R.id.webview);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);

        webView.setWebViewClient(new WriteHandlingWebViewClient(webView) {
        });
        webView.setWebChromeClient(new WebChromeClient());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
//                如果是get请求，直接拦截url请求即可
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                try {
                    URL url = new URL(request.getUrl().toString());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                Log.e("InternetActivity", request + "");
                return super.shouldInterceptRequest(view, request);
            }

        });
        swipeRefreshLayout = findViewById(R.id.SwipeRefreshLayout);
        swipeRefreshLayout.setColorSchemeResources(R.color.black);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadUrl();
            }
        });
        swipeRefreshLayout.setRefreshing(true);
        webView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN){
                    if(keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()){
                        //表示按返回键时的操作
                        webView.goBack();
                        return true;
                    }
                }
                return false;
            }
        });

        loadUrl();
    }

    private void loadUrl(){
        HttpRequest.getString("https://fsssw.com/url.txt", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.d("测试", s);
                swipeRefreshLayout.setRefreshing(false);
                if(s.contains("http")){
                    Toast.makeText(MainActivity.this,"正在访问："+s,Toast.LENGTH_SHORT).show();
                    webView.loadUrl(s);
                }else{
                    Toast.makeText(MainActivity.this,s + "---这不是个网址",Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("测试", volleyError.getMessage());
                Toast.makeText(MainActivity.this,"网络错误："+volleyError.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private long firstTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                System.exit(0);
            } else {
                Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}